package com.dingshi.security.adapter;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Base64InputStream;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.dingshi.security.R;

public class GridAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<String> list;
	public GridAdapter(Context context,ArrayList<String> list) {
		this.context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		return list!=null?list.size():0;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		String data = list.get(position);
		ImageView image = null;
		if (convertView == null) {
			image = new ImageView(context);
			convertView = image;
			convertView.setTag(image);
		} else {
			image = (ImageView) convertView.getTag();
		}
		byte[] buffer = data.getBytes();
		Base64InputStream ips = new Base64InputStream(new ByteArrayInputStream(buffer), Base64.DEFAULT);
		Bitmap bitmap = BitmapFactory.decodeStream(ips);
		if(bitmap!=null) {
			image.setImageBitmap(bitmap);
		}else {
			image.setImageResource(R.drawable.dail_default);
		}
		return convertView;
	}
}
